<template>
  <div>
    <BasicModal
      @register="register"
      title="字典数据"
      width="900px"
      :destroyOnClose="true"
      :canFullscreen="false"
      :showOkBtn="false"
      :showCancelBtn="false"
    >
      <BasicTable @register="registerTable">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <TableAction
              :actions="[
                {
                  label: '编辑',
                  tooltip: '编辑',
                  onClick: handleAdd.bind(null, 2, record),
                },
                {
                  label: '删除',
                  tooltip: '删除',
                  onClick: handleDel.bind(null, record),
                },
              ]"
          /></template>
        </template>
        <template #toolbar>
          <a-button type="primary" @click="handleAdd(1, null)">新建</a-button>
        </template>
        <template #status="{ column, record }">
          <Badge
            v-if="column.dataIndex === 'status'"
            :status="statusMap[record.status].status"
            :text="statusMap[record.status].text"
          />
        </template>
      </BasicTable>
    </BasicModal>
    <DataAddedModal @register="registerAdd" @reload="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { BasicModal, useModal, useModalInner } from '@/components/Modal';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import DataAddedModal from './dataAdded.vue';
  import { Badge, Modal } from 'ant-design-vue';
  import { ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { getBasicColumns } from './tableData';
  import { DictDataList, DictDataRemove } from '@/api/platform/dictionary';
  import { IDictDataListResult } from '@/api/platform/dictionary/type';

  const { createMessage } = useMessage();
  const dictType = ref();

  const statusMap = ref({
    0: {
      status: 'success',
      text: '正常',
    },
    1: {
      status: 'default',
      text: '禁用',
    },
  });

  const [register] = useModalInner((data) => {
    dictType.value = data.dictType;
  });

  const [registerTable, { reload }] = useTable({
    title: '',
    api: DictDataList,
    columns: getBasicColumns(),
    rowKey: 'dictCode',
    useSearchForm: false,
    canResize: false,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: true,
    clickToRowSelect: false,
    actionColumn: {
      width: 100,
      title: '操作',
      dataIndex: 'action',
    },
    beforeFetch: (params) => {
      return {
        ...params,
        dictType: dictType.value,
      };
    },
  });

  const [registerAdd, { openModal }] = useModal();

  function handleAdd(type, row) {
    openModal(true, {
      type,
      row,
      dictType: dictType.value,
    });
  }

  function handleDel(row: IDictDataListResult) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该字典数据',
      onOk: async () => {
        await DictDataRemove(row.dictCode);
        createMessage.success('删除成功！');
        reload();
      },
    });
  }
</script>

<style lang="less" scoped></style>
